$(function() {
	// 验证
	validateInit();

	$('#deviceTable').bootstrapTable({
		data : getDeviceDatas(),
		striped : true, 
		cache : false, 
		pagination : true, 
		sortable : false, 
		sidePagination : "client",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 25, 50, 100 ], 
		search : true, 
		strictSearch : false,
		//showColumns : true, 是否有选择列的那个选项
		clickToSelect : true,
		uniqueId : 'id',
		columns : [ 
		{
			field : 'deviceName',
			title : '设备名称'
		}],
		onClickRow : function(row) {// 单击查看
			$("#deviceId").val(row.id);
			$("#deviceAttrTable").bootstrapTable("load", getDeviceAttrDatas());
		}
	});
	$("#deviceTable").bootstrapTable("hideColumn","id");
	
	
	$('#deviceAttrTable').bootstrapTable({
		data : getDeviceAttrDatas(),
		striped : true,
		cache : false,
		pagination : true,
		sortable : false,
		toolbar : "#deviceAttrToolbar",
		sidePagination : "client",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 25, 50, 100 ],
		search : true,
		strictSearch : false,
		showColumns : true,
		clickToSelect : true,
		uniqueId : 'id',
		columns : [
		{
			field : 'attrName',
			title : '参数名'
		}, {
			field : 'attrValue',
			title : '参数值'
		}, {
			field : 'operate',
			title : '操作'
		} ]
	});

	$("#deviceAttrTable").bootstrapTable("hideColumn", "id");
});

//表单验证
function validateInit() {
	//修改表单校验
		$("#updateDeviceAttrForm").validate({
			rules : {
				attrValue: {
					required : true 
				}
			},
			success : function(label, element) {// error是lavel标签，element是input标签
				validSuccess(element,label);
			},
			errorPlacement : function(label, element) {
				validError(element,label);
			}
		});
}

//获得所有的设备名
function getDeviceDatas() {
	//所有的设备名
	var device = '';
	$.ajax({
		url: "../device/query.action",
		type: "post",
		data: {"deviceStatus":"1"},
		async: false,//是为了内层ajax在之后访问
		success: function(data) {
			$("#deviceId").val(data[0].id);
			device = data;
		},
		error: function() {
			toastr.error("获取设备信息出错，请联系管理员");
		}
	});
	return device;
}
//通过设备名获得设备信息
function getDeviceAttrDatas() {
	var deviceAttrs;
	var deviceId = $("#deviceId").val();
	$.ajax({
		url:"../deviceAttr/query.action",
		type: "post",
		data: {"deviceId":deviceId, "attrStatus":"1"},
		async: false,
		success: function(data) {
			$(data).each(function() {
				this.attrName = getAttrNameName(this.attrNameId);
				//操作赋值	
				this.operate='<button class=\'btn btn-primary btn-xs\' onclick=\'updateDeviceAttr("' + this.id + '","'+ this.attrName + '","' + this.attrValue +'")\'>修改</button>&nbsp;';
			});
			deviceAttrs = data;
		}
	});
	return deviceAttrs;
}

function returnHome() {
	window.location.href="../device/page.action";
}


// 修改
function updateDeviceAttr(id, attrName, attrValue) {
	formReset("#updateDeviceAttrForm");
			
	$("#updateDeviceAttrForm [name='id']").val(id);
	$("#attrNameLabel").html(attrName+"：");
	$("#updateDeviceAttrForm [name='attrValue']").val(attrValue);
	
	$("#updateDeviceAttrModal [onclick='updateDeviceAttrSave()']").attr("disabled", "disabled");
	
	$("#updateDeviceAttrForm input").bind('input', function(){
		$("#updateDeviceAttrModal [onclick='updateDeviceAttrSave()']").removeAttr("disabled");
	});
			
	$("#updateDeviceAttrModal").modal("show");
}
function updateDeviceAttrSave() {
	if (!$("#updateDeviceAttrForm").valid()) {
		return false;
	}
	var data =$("#updateDeviceAttrForm").serialize(); 
	//alert(data.attrValue);
	$.ajax({
		url : "../deviceAttr/update.action",
		type : "post",
		data : data,
		success : function(data) {
			if (data.success == "true") {
				toastr.success("更新成功");
				$("#deviceAttrTable").bootstrapTable("load", getDeviceAttrDatas());
			} else {
				toastr.error("后台处理发生错误，请联系管理员");
			}
		},
		error : function() {
			toastr.error("提交请求发生错误，请联系管理员");
		}
	});
	$("#updateDeviceAttrModal").modal("hide");
}

//设备名下拉框显示类别
function suggest(){
    		$.ajax({
    			url: "../device/getAllDeviceInfo.action",
    			type: "post",
    			success: function(data) {
    				console.log(data);
    				var array = [];
    				for(var i=0;i<data.length;i++){
    				     var temp = {"deviceName":data[i].deviceName};
    				     array.push(temp);
    				}
    				//console.log(array);
    				    //添加设备的下拉框
						var testBsSuggest = $("#testNoBtn").bsSuggest({
						    data:array,
						    showBtn: false,
						    idField: "userId",
						    //keyField: "catgorName",
						    indexKey: 0,
						    effectiveFieldsAlias: {}, 
						    listHoverStyle: 'background: #18a689; color:#fff'
						}).on('onDataRequestSuccess', function (e, result) {
						    console.log('onDataRequestSuccess: ', result);
						}).on('onSetSelectValue', function (e, keyword) {
						    console.log('onSetSelectValue: ', keyword);
						}).on('onUnsetSelectValue', function (e) {
						    console.log("onUnsetSelectValue");
						});
						//修改设备的属性下拉框
						var testBsSuggest = $("#testNoBtn1").bsSuggest({
						    data:array,
						    showBtn: false,
						    idField: "userId",
						    //keyField: "catgorName",
						    indexKey: 0,
						    listHoverStyle: 'background: #18a689; color:#fff'
						}).on('onDataRequestSuccess', function (e, result) {
						    console.log('onDataRequestSuccess: ', result);
						}).on('onSetSelectValue', function (e, keyword) {
						    console.log('onSetSelectValue: ', keyword);
						}).on('onUnsetSelectValue', function (e) {
						    console.log("onUnsetSelectValue");
						});
    			},
    			error: function() {
    			}
    		});
}
function suggest1(){
	$.ajax({
		url: "../attr/getAllAttr.action",
		type: "post",
		success: function(data) {
			console.log(data);
			var array = [];
			for(var i=0;i<data.length;i++){
			     var temp = {"attrName":data[i].attrName};
			     array.push(temp);
			}
			//console.log(array);
			    //添加设备的下拉框
				var testBsSuggest = $("#testNoBtn").bsSuggest({
				    data:array,
				    showBtn: false,
				    idField: "userId",
				    //keyField: "catgorName",
				    indexKey: 0,
				    effectiveFieldsAlias: {}, 
				    listHoverStyle: 'background: #18a689; color:#fff'
				}).on('onDataRequestSuccess', function (e, result) {
				    console.log('onDataRequestSuccess: ', result);
				}).on('onSetSelectValue', function (e, keyword) {
				    console.log('onSetSelectValue: ', keyword);
				}).on('onUnsetSelectValue', function (e) {
				    console.log("onUnsetSelectValue");
				});
				//修改设备的类别下拉框
				var testBsSuggest = $("#testNoBtn1").bsSuggest({
				    data:array,
				    showBtn: false,
				    idField: "userId",
				    //keyField: "catgorName",
				    indexKey: 0,
				    listHoverStyle: 'background: #18a689; color:#fff'
				}).on('onDataRequestSuccess', function (e, result) {
				    console.log('onDataRequestSuccess: ', result);
				}).on('onSetSelectValue', function (e, keyword) {
				    console.log('onSetSelectValue: ', keyword);
				}).on('onUnsetSelectValue', function (e) {
				    console.log("onUnsetSelectValue");
				});
		},
		error: function() {
		}
	});
}

